package com.joplus.housekeeper.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Interpolator;

import com.joplus.housekeeper.R;


/**
 * Created by Administrator on 2016/8/9.
 */
public class YoYoLine extends View {


    String TAG = "YoYoLine";
    Paint paint;
    Line line;

    Point touchPoint = new Point();
    Point movePoint = new Point();
    Point upPoint = new Point();
    int width, height;
    Bitmap bitmap;
    Bitmap anglebitmap;
    Drawable drawable;
    int lightcolor,heightcolor;
    View view;
    StageCallback callback;
    Context mcontext;

    Boolean isend=false;
    int num=0;
    Handler handler=new Handler(){
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (num){
                case 1:

                    lightcolor=line.createColor(true);
                    heightcolor=line.createColor(false);

                    break;
                case 2:
                    int i=lightcolor;
                    lightcolor=heightcolor;
                    heightcolor=i;

                    break;
                case 3:
                    heightcolor=lightcolor;
                    num=0;
                    break;
                default:
                    num=0;
                    break;
            }
            invalidate();
            if(!isend){
                //handler.sendEmptyMessageDelayed(0,50);
            }


        }
    };

    public void setStageCallback(StageCallback callback) {
        this.callback = callback;
    }

    public YoYoLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        drawable = context.getResources().getDrawable(R.drawable.login_logo);
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        //可以在调用getBitmap方法，得到这个位图
        bitmap = bitmapDrawable.getBitmap();

        drawable = context.getResources().getDrawable(R.drawable.login_angle);
        anglebitmap=((BitmapDrawable) drawable).getBitmap();
        anglebitmap=Scal(anglebitmap,0.2f);
        mcontext=context;
        paint = new Paint();
        paint.setShadowLayer(50, 30, 30, 0xFFFF00FF);
        paint.setColor(Color.rgb(0, 255, 0));
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(10);
        paint.setDither(true);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                if(isend){
                   callback.stage(true);
                    return false;
                }
                Log.e(TAG, event.toString() + "\n" + line.getMapPosition().toString());


                if (Math.abs((int) event.getX() - line.getMapPosition().x) < dip2px(mcontext,45) && Math.abs((int) event.getY() - line.getMapPosition().y) < dip2px(mcontext,45)) {
                    touchPoint.set((int) event.getX(), (int) event.getY());
                    return true;
                } else {
                    return false;
                }
            case MotionEvent.ACTION_MOVE:
                isend=true;
                movePoint.set((int) event.getX(), (int) event.getY());
                if (movePoint.y < height / 4) {
                    line.setRect(0);
                    callback.stagePage(0);
                    line.setLineEY(height / 4);
                } else if ((movePoint.y) < height * 2 / 5) {
                    line.setRect(0);
                    callback.stagePage(0);
                    line.setLineEY(movePoint.y);
                } else if ((movePoint.y) < height * 3 / 5) {
                    line.setRect(movePoint.y - height *2/ 5);
                    line.setLineEY(movePoint.y);
                    callback.stagePage(movePoint.y - height *2/ 5);
                } else if((movePoint.y) < height * 4 / 5){
                    line.setRect(movePoint.y - height*2 / 5);
                    callback.stagePage(movePoint.y - height *2/ 5);
                    line.setLineEY( height * 3 / 5);
                }else {
                    line.setRect(height);
                    callback.stagePage(height);
                    line.setLineEY(height * 3 / 5);
                }
//                else{
//                    line.setRect(0);
//                    line.setLineEY(movePoint.y);
//                }
                if(line.getRect()>height/2){
                    line.setRect(height);
                    callback.stagePage(height);
                }
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                Log.e(TAG, event.getY() + "\n" + getHeight());
                upPoint.set((int) event.getX(), (int) event.getY());
                if ((line.getRect()) < height /2) {
                    line.setRect(0);
                    callback.stagePage(0);
                    line.setLineEY(height / 4);
                    callback.stage(false);
                    isend=false;
                    invalidate();
                } else  {
                    isend=true;
                    callback.stage(true);
                    line.setRect(getHeight());
                    callback.stagePage(getHeight());
                    line.setLineEY(height / 4);
                    invalidate();
                }
                break;

        }
        return true;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Log.d(TAG, line.toString());
        line.drawLine(paint, canvas, line);
        if(!isend){
            handler.sendEmptyMessageDelayed(0,500);
        }else{

        }

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
        line = new Line(width, height, bitmap);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);

    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
       drawable=null;
    }

    public interface StageCallback {
        public void stage(Boolean stage);
        public void stagePage(int yposition);
    }

    public void deinit(){
        // 销毁时调用
        if(bitmap != null && !bitmap.isRecycled()){
            bitmap.recycle();
            bitmap = null;
        }
    }

    private class Line {
        private int lineSX;//线的开始横坐标
        private int lineSY;//线的开始纵坐标
        private int lineEX;//线的结束横坐标
        private int lineEY;//线的结束纵坐标

        int angleft=30;

        private int lineSLeng = 50;//线的开始长度
        private int lineELeng = 150;//线的结束长度
        Bitmap bitmap;
        int bitmapWidth;
        int bitmapHeight;
        Point path1S;//三角路径1起点
        Point path2S;//三角路径2起点

        Point angle;//三角定点

        Path upAng;
        Path downAng;

        Rect rect;


        public Point getMapPosition() {
            Point point = new Point();
            point.set(lineEX, lineEY + bitmapHeight / 2);
            return point;
        }

        public int getRect() {
            return rect.bottom;
        }

        public void setRect(int bottom) {
//            if(bottom<lineSLeng){
//                return;
//            }

            rect.set(0, 0, getWidth(), bottom);
        }

        public int getLineSX() {
            return lineSX;
        }

        public int getLineSY() {
            return lineSY;
        }

        public int getLineEX() {
            return lineEX;
        }

        public int getLineEY() {
            return lineEY;
        }

        public int getLineSLeng() {
            return lineSLeng;
        }

        public int getLineELeng() {
            return lineELeng;
        }

        public void setLineSX(int lineSX) {
            this.lineSX = lineSX;
        }

        public void setLineSY(int lineSY) {
            this.lineSY = lineSY;
        }

        public void setLineEX(int lineEX) {
            this.lineEX = lineEX;
        }

        public void setLineEY(int lineEY) {
            this.lineEY = lineEY;
            path1S = new Point(lineEX - angleft, this.lineEY + bitmapHeight / 2 + 30);
            path2S = new Point(lineEX - angleft, this.lineEY + bitmapHeight / 2 + 60);
            upAng = new Path();
            upAng.moveTo(path1S.x, path1S.y);
            upAng.lineTo(path1S.x + angleft, path1S.y + 20);
            upAng.lineTo(path1S.x + angleft*2, path1S.y);
            downAng = new Path();
            downAng.moveTo(path2S.x, path2S.y);
            downAng.lineTo(path2S.x + angleft, path2S.y + 20);
            downAng.lineTo(path2S.x + angleft*2, path2S.y);
        }

        public void setLineSLeng(int lineSLeng) {
            this.lineSLeng = lineSLeng;
        }

        public void setLineELeng(int lineELeng) {
            this.lineELeng = lineELeng;
        }

        public Line(int width, int height, Bitmap bitmap) {

            lineSLeng = height / 2;
            lineSX = width / 2;
            lineSY = 0;
            lineEX = width / 2;
            lineEY = height / 4;
            touchPoint = new Point(width / 2, height / 4);
            rect = new Rect(0, 0, width, 0);

            this.bitmap = bitmap;
            bitmapHeight = bitmap.getHeight();
            bitmapWidth = bitmap.getWidth();
            path1S = new Point(lineEX - angleft, lineEY + bitmapHeight / 2 + 30);
            path2S = new Point(lineEX - angleft, lineEY + bitmapHeight / 2 + 60);
            upAng = new Path();
            upAng.moveTo(path1S.x, path1S.y);
            upAng.lineTo(path1S.x + angleft, path1S.y + 20);
            upAng.lineTo(path1S.x + angleft*2, path1S.y);
            downAng = new Path();
            downAng.moveTo(path2S.x, path2S.y);
            downAng.lineTo(path2S.x + angleft, path2S.y + 20);
            downAng.lineTo(path2S.x + angleft*2, path2S.y);
            lightcolor=createColor(true);
            heightcolor=createColor(false);
        }


        /**
         * @param p
         * @param c
         * @param L
         */
        public void drawLine(Paint p, Canvas c, Line L) {


            num++;
            p.setColor(Color.rgb(255, 255, 255));
            c.drawLine(L.lineSX, L.lineSY, L.lineEX, L.lineEY, p);
            c.drawBitmap(bitmap, line.getLineSX() - bitmapWidth / 2, line.getLineEY() - bitmapHeight / 2, paint);
            c.drawBitmap(anglebitmap,line.getLineSX() - anglebitmap.getWidth()/ 2,rect.bottom,paint);
            Paint p2 = new Paint();
            p2.setAntiAlias(true);
            p2.setStrokeWidth(5);
            p2.setColor(lightcolor);
            p2.setStyle(Paint.Style.STROKE);
            drawPath(c, upAng, p2);
            p2.setColor(heightcolor);
            drawPath(c, downAng, p2);
            c.drawRect(rect, p);

        }

        public void drawPath(Canvas c, Path path, Paint p) {
            c.drawPath(path, p);
        }

        public  int createColor(Boolean light) {

            return light? Color.rgb(255, 255, 255) : Color.rgb(100, 100, 100);
        }

        @Override
        public String toString() {
            return "Line{" +
                    "lineSX=" + lineSX +
                    ", lineSY=" + lineSY +
                    ", lineEX=" + lineEX +
                    ", lineEY=" + lineEY +
                    ", lineSLeng=" + lineSLeng +
                    ", lineELeng=" + lineELeng +
                    '}';
        }

    }

    private  Bitmap Scal(Bitmap bitmap,float scal) {
        Matrix matrix = new Matrix();
        matrix.postScale(scal, scal); //长和宽放大缩小的比例
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        return resizeBmp;
    }

    Interpolator interpolator = new Interpolator() {
        @Override
        public float getInterpolation(float input) {
            return 0;
        }
    };

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


}
